package com.example.mypra1.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mypra1.R;
import com.szysky.customize.siv.SImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.InnerHolder> {

    //从数据源获取的数组存入list，demo的数据源是模拟，在代码中生成
    private List<String> list;

    /**
     * 点击、长按监听三件套
     */
    //1.定义接口
    public interface OnClickListener {
        void onClick(View v, int position);
    }

    public interface OnLongClickListener {
        void onLongClick(View v, int position);
    }

    //2.声明接口
    private OnClickListener onClickListener;
    private OnLongClickListener onLongClickListener;

    //3.set接口
    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        this.onLongClickListener = onLongClickListener;
    }

    //构造器，只留这一个构造器，将list传入
    public RecycleAdapter(List<String> list) {
        this.list = list;
    }

    /**
     * 重写三件套，快捷键生成，
     * 观看每个方法的内部代码
     */
    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //将XML布局文件，加载到Java代码（和Activity的onCreate()的setContentVIew()作用一样），参数先复制粘贴，感兴趣可去研究传不同参数的效果
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_item, null, false);
        //设置背景也可以在XML中设置
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, @SuppressLint("RecyclerView") int position) {
        //给每一个item设置不同的数据，从list获取
        holder.sImageView.setImageUrls(list.get(position));
        Log.d("hahahah","xixixixixix");

        //给成员接口变量（onClickListener，onLongClickListener）进行非空判断，非空触发回调
        if (onClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.onClick(v, position);
                }
            });
        }
        if (onLongClickListener != null) {
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    onLongClickListener.onLongClick(v, position);
                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


    /**
     * 内部类，继承RecyclerView.ViewHolder，作用就是声明item中的控件，findViewById
     */
    public class InnerHolder extends RecyclerView.ViewHolder {
        SImageView sImageView;

        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            sImageView = itemView.findViewById(R.id.img);
        }
    }


}

